from requests_html import HTMLSession
import pyperclip
import os

'''
http://www.dbqu365.com
http://www.dbqu365.com/47/47206/
'''

# 获取请求对象
session = HTMLSession()
url = pyperclip.paste()
# 往网站发送get请求
page = session.get(url)
page.encoding = 'utf-8'
content = page.html.find('ul.list-charts', first=True)
name = page.html.find('h1.text-center', first=True).text
# 下载地址
path = r'D:\.爬取为txt\{_name}\ '
path = path.format(_name=name)
if(os.path.exists(path) == False):
    os.makedirs(path)

a_s = content.find('a')
# a_s = a_s[::-1]
for a in a_s:
    href = a.attrs['href']
    _a = 'http://www.dbqu365.com' + href
    _page = session.get(_a)
    _page.encoding = 'utf-8'
    chap = _page.html.find('div.chaptertitle', first=True).text
    chap = chap.replace('字体设置', '')
    print('a ', chap, _a)
#     text = _page.html.find('div#chaptercontent', first=True).text
#     # print(_page.html.find('div#chaptercontent', first=True).text)
#     href_2 = href.replace('.html', '_2.html')
#     text_2 = _page.html.find('div#chaptercontent', first=True).text
#     with open(path + f'{name}.txt', 'a', encoding='utf-8') as f:
#         f.write('\n')
#         f.write(chap)
#         f.write('\n\n')
#         f.write(text)
#         f.write(text_2)